package org.itboys.weixin.web.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mongodb.utils.page.Page;
import org.itboys.mongodb.utils.query.QueryParamUtils;
import org.itboys.weixin.entity.WeixinMessage;
import org.itboys.weixin.service.WeixinMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.google.common.collect.Maps;

@RestController
@RequestMapping(value = "/weixin/message")
public class WeixinMessageController extends BaseController {

	@Autowired
	private WeixinMessageService weixinMessageService; 
	@Autowired
	private ResourceHolder resourceHolder;
	
	@RequestMapping("/vm/{weixinId}")
	public ModelAndView projectMenus(@PathVariable("weixinId") Long weixinId,HttpServletRequest request,Model model){
		model.addAttribute("weixinId",weixinId);
		return new ModelAndView("/weixin/message/list");
	}
	
	@RequestMapping("/list")
	public void list(HttpServletResponse response, HttpServletRequest request){
		final Map<String,Object> sqlMap = QueryParamUtils.builderQueryMap(request);
		Page<WeixinMessage> page = weixinMessageService.pageQuery2(request, sqlMap);
		JsonPageUtils.renderJsonPage(page.getTotal(),page.getData(),response);
	}
	
	
	@RequestMapping("/getById")
	public ModelAndView getById(HttpServletRequest request,HttpServletResponse response,Model model){
		String id = request.getParameter("id");
		String weixinId = request.getParameter("weixinId");
		model.addAttribute("weixinId", weixinId);
		if(StringUtils.isNotBlank(id)){
			WeixinMessage entity = weixinMessageService.getById(Long.valueOf(id));
			model.addAttribute("entity", entity);
		}
		/*model.addAttribute("staticRoot",resourceHolder.getStringValue("staticRoot"));
		model.addAttribute("adminRoot",resourceHolder.getStringValue("adminRoot"));
		model.addAttribute("imgRoot",resourceHolder.getStringValue("imgRoot"));*/
		return  new ModelAndView("/weixin/message/input");
	}
	
	@RequestMapping("/save")
	public void insert(HttpServletResponse response, @ModelAttribute WeixinMessage weixinMessage){
		try {
			if(weixinMessage.getId()==0L){
				if(weixinMessage.getType()==1){
					Map<String,Object> sqlMap=Maps.newHashMap();
					sqlMap.put("type", 1);
					sqlMap.put("weixinId", weixinMessage.getWeixinId());
					if(weixinMessageService.list(sqlMap).size()>0){
						AjaxUtils.renderText(response, CommonConstants.FAIL);
					}else{
						weixinMessageService.save(weixinMessage);
					}
					AjaxUtils.renderText(response, CommonConstants.SUCCESS);
				}else{
					weixinMessageService.save(weixinMessage);
					AjaxUtils.renderText(response, CommonConstants.SUCCESS);
				}
			}else{
				weixinMessageService.update(weixinMessage);
				AjaxUtils.renderText(response, CommonConstants.SUCCESS);
			}
		} catch (Exception e) {
			logger.error("weixinmessage insert error", e);
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}
	
	@RequestMapping("/delete")
	public void delete(HttpServletResponse response,HttpServletRequest request){
		try{
			Long id =  Long.valueOf(request.getParameter("id"));
			AjaxUtils.renderText(response, String.valueOf(weixinMessageService.delete(id)));
		}catch (Exception e) {
			logger.error("weixinmessage delete error", e);
			AjaxUtils.renderJson(response, 0);
		}
	}
	
	@RequestMapping("/getlist")
	public void getList(HttpServletResponse response,HttpServletRequest request){
		try{
			Long weixinId =  Long.valueOf(request.getParameter("weixinId"));
			Map<String ,Object> sqlMap=Maps.newHashMap();
			sqlMap.put("weixinId", weixinId);
			List<WeixinMessage> wmlist=weixinMessageService.list(sqlMap);
			AjaxUtils.renderJson(response,wmlist);
		}catch (Exception e) {
			logger.error("weixinmessage getList error", e);
			AjaxUtils.renderJson(response, 0);
		}
	}
}
